package org.ricks.db;

import org.ricks.common.Assert;
import org.ricks.common.bean.ArrayUtil;
import org.ricks.common.db.BaseDao;
import org.ricks.ioc.bean.factory.DaoContext;
import org.ricks.utils.ListUtil;

import java.util.List;

/**
 * @author chenwei
 * @Description:增删改查最外层DB操作
 * @date 2020/12/2117:23
 */
public class EntityMgr {

    /**
     * 保存数据
     * @param clazz
     * @param ts
     * @param <T>
     */
    public static<T> void save(Class clazz, List<T> ts) {
        BaseDao baseDao = DaoContext.get(clazz);
        Assert.isNotNull(baseDao," dao not found, from  {}  fuck you why ！",clazz.getName());
        baseDao.save(ArrayUtil.toArray(ts,clazz));
    }

    /**
     * 删除数据
     * @param clazz
     * @param object
     * @param <T>
     */
    public static<T> void delete(Class clazz,T... object) {
        BaseDao baseDao = DaoContext.get(clazz);
        Assert.isNotNull(baseDao," dao not found, from  {}  fuck you why ！",object.getClass().getName());
        baseDao.delete(object);
    }

    /**
     * 更新数据
     * @param clazz
     * @param objects
     * @param <T>
     */
    public static<T> void update(Class clazz,T... objects) {
        BaseDao baseDao = DaoContext.get(clazz);
        Assert.isNotNull(baseDao," dao not found, from  {}  fuck you why ！",clazz.getClass().getName());
        baseDao.update(objects);
    }

    /**
     * 查询一条数据
     * @param t
     * @param fileds
     * @param <T>
     * @return
     */
    public static<T> T get(T t,String... fileds) {
        BaseDao baseDao = DaoContext.get(t.getClass());
        Assert.isNotNull(baseDao," dao not found, from  {}  fuck you why ！",t.getClass().getName());
        return (T) baseDao.get(t,fileds);
    }


    /**
     * 查询所有
     * @param t
     * @param <T>
     * @return
     */
    public static<T> List<T> list(T t,String... fileds) {
        BaseDao baseDao = DaoContext.get(t.getClass());
        Assert.isNotNull(baseDao," dao not found, from  {}  fuck you why ！",t.getClass().getName());
        return baseDao.queryList(t,fileds);
    }
}
